/**
 * 
 * @param {Object} args
 * args[ 
 * title , height , width , url , param ,_callback
 * ]
 */
function MyWindow( args ){
	this.init( args );
}

MyWindow.prototype = {
	init : function( args ){
		this.title = args.title;
		this.height = args.height;
		this.width = args.width < 440 ? 440 : args.width;
		this.url = args.url;
		this.param = args.param ? args.param : '';
		this._callback = args._callback;
		this.winId = 'window_' + this.title;
		this.id = "#" + this.winId;
		this.appendWindow();
		this.fillWindow( this.url, this.param );
		this.openWindow();
	},
	appendWindow:function(){
		$(document.body).append( "<div style='display:none;'><div id='" 
			+ this.winId + "' style='width:"+ this.width+"px;height:" 
			+ this.height+ "px;'></div></div>" );
		$(this.id).data( "myWindow" , this );
	},
	fillWindow:function( param ){
		htmlSend( this.url , param , this.id , false );
		this.registerEvent();
	},
	registerEvent:function(){
		var target = this;
		$(this.id ).find('.list').children('tbody').children('tr').mouseover(function(){
			$(this).css( "background" , "#FBEC88" );
		});
		$( this.id  ).find('.list').children('tbody').children('tr').mouseout(function(){
			$(this).css( "background" , "#FFFFFF" );
		});
		$( this.id  ).find('.list').children('tbody').children('tr').dblclick(function(){
			var data = [];
			$(this).children("td").each( function(index){
			var thead = $(this).parent().parent().parent().children( "thead").children("tr").children("th" );
				data[ $(thead.get(index)).html() ] = $(this).html();
			});
			target._callback( data );
			target.closeWindow();
		});
	},
	openWindow:function(param ,refresh ){
		if( refresh ){
			this.fillWindow( param );
		}
		$( this.id ).window({title:this.title,width:this.width,height:this.height,modal:true,onClose:function(){
			showSelect();
		}});
		$( this.id ).window( 'open' );
		hiddenSelect();
	},
	closeWindow:function(){
		$(this.id).window( 'close' );
	}
}
/**
 *
 * @param {Object} title
 * @param {Object} url
 * @param {Object} width
 * @param {Object} height
 * @param {Object} _callback
 */
var choicewindows = [];
function showWindow( title , url , width , height , _callback , param , refresh ){
	var myWindow = choicewindows[title];
	if( myWindow ){
		myWindow.openWindow( param , refresh );
	}else{
		myWindow = new MyWindow({
			title : title ,
			url : url,
			width: width ,
			height:height ,
			_callback:_callback,
			param : param,
			refresh : refresh
		});
		choicewindows[title] = myWindow;
	}
}
